<?php
class SystemModel extends Model {
	
	public function labelList(){
		$label = L('label');
		$result = array();
		foreach($label AS $group=>$lab){
			$result[$group]['list'] = $this->valueList($group);
			$result[$group]['name'] = $lab;
			$result[$group]['id'] = $group;
		}
		return $result;
	}
	
	private function valueList($group=1){
		$value = $this->where("`group`=".$group)->order("`sort` ASC")->findAll();
		foreach ($value AS $key=>$val){
			if($val['value_list']){
				$vl = explode("\n", $val['value_list']);
				foreach ($vl AS $k=>$v){
					$tv = explode("|", $this->rebr($v));
					$value[$key]['list'][$k]['value'] = $this->rebr($tv[1]);
					$value[$key]['list'][$k]['name'] = $tv[0];
					if($val['value'] == $this->rebr($tv[1])){
						$value[$key]['list'][$k]['selected'] = true;
					}else{
						$value[$key]['list'][$k]['selected'] = false;
					}
				}
			}
		}
		return $value;
	}
	
	private function rebr($str){
		$str = preg_replace("/\s/", "", $str);
		return $str;
	}
	
	public function configList(){
		$value = $this->order("`sort` ASC")->field("var, value")->findAll();
		return $value;
	}
	
	public function updateValue($val){
		if(is_array($val) && !empty($val)){
			foreach ($val AS $key=>$v){
				$this->where("id=".$key)->save(array('value'=>$v));
			}
		}
		return;
	}
}
?>